const app = getApp()
import util from '../../../utils/util'
import {
  GetGriderEventList,
  GetWsxfList,
  GetIsseuChuLiEventList,
  GetShiveList,
  GetIsseuGriderList
} from '../../../config/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '', // 下级处理type
    isEmpty: false, // 是否为空
    isBottom: false, // 是否无数据触底了
    isReachBtm: false, // 是否触发上拉加载
    grider: '', // 网格员信息
    cat_id: 2, // 2走访排查3村民上报
    grider_id: 0, //筛选下级人员id
    status: '', //1已解决2正在处理0待解决
    time: '', //月份
    ucid: 0, //事件类型
    page: 1,
    limit: 10,
    lists: [],
    uc_name: "全部类型",
    showView: true,
    ucList: [], //事件类型数组
    status_name: "全部状态",
    showViewStatus: true,
    isseuGriderList: [], //下级列表
    isseuGrider: [],
    showGriderView: true,
    griderChageIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let grider = wx.getStorageSync('grider');
    // grider={
    //   id:8
    // }
    this.setData({
      grider: grider,
      grider_id: grider.id,
    })
    this.getYearMonth();

    this.getList()

    this.getIsseuGriderList(this.data.grider_id);
  },

  getList() { // 获取列表
    let that = this;

    wx.showLoading({
      title: '玩命加载中',
    })

    let params = {
      id: this.data.grider.id,
      vollege_id: app.globalData.vollege_id,
     // cat_id: this.data.cat_id,
      grider_id: this.data.grider_id,
    //  status: this.data.status,
    // time: this.data.time,
    //  ucid: this.data.ucid,
      page: this.data.page,
      limit: this.data.limit
    }
    util.apiRequest(GetIsseuChuLiEventList, params, null).then(r => {
      console.log('2事件列表', r)
      if (r.data.code == 0) {
        wx.showToast({
          title: r.data.info,
          icon: 'none',
          duration: 2000
        })
        that.setData({
          isEmpty: true
        })
        return false;
      }
      var newList = that.data.lists.concat(r.data.data)

      if (r.data.data == '' && that.data.isReachBtm == true) {
        that.setData({
          isBottom: true
        })
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      }

      that.setData({
        lists: newList
      })

      wx.hideLoading();

      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }

    })
  },

  //村民上报button切换事件
  setm(e) {
    let curIndex = e.currentTarget.dataset.index
    console.log('村民上报切换', curIndex)
    this.setData({
      isBottom: false,
      isReachBtm: false,
      cat_id: curIndex,
      lists: [],
      page: 1
    })
    this.getList();
  },
  //弹出事件类型选择遮罩层
  ucChage(e) {
    let that = this;
    util.apiRequest(GetShiveList, {
      vollege_id: app.globalData.vollege_id
    }, null).then(r => {
      console.log('事件类型', r)
      that.setData({
        showView: false,
        ucList: r.data.data
      })
    })


  },
  //事件类型选择确定
  ucSetm(e) {
    let id = e.currentTarget.dataset.id
    let title = e.currentTarget.dataset.title
    this.setData({
      ucid: id,
      uc_name: title,
      lists: [],
      page: 1,
      showView: true
    })
    this.getList();
  },

  //弹出状态选择遮罩层
  statusChage(e) {
    let that = this;
    that.setData({
      showViewStatus: false
    })
  },
  //事件状态选择确定
  statusSetm(e) {
    let id = e.currentTarget.dataset.id
    let title = e.currentTarget.dataset.title
    this.setData({
      status: id,
      status_name: title,
      lists: [],
      page: 1,
      showViewStatus: true
    })
    this.getList();
  },
  //日期选择事件
  bindDateChange(e) {
    console.log("日期选择事件", e)
    let time = e.detail.value
    this.setData({
      time: time,
      lists: [],
      page: 1,
    })
    this.getList();
  },
  //调用下级人员
  getIsseuGriderList(id) {
    let that = this;
    util.apiRequest(GetIsseuGriderList, {
      id: id
    }, null).then(r => {
      console.log('下级人员1', r)
      if (r.data.data.length > 0) {
        let isseuGriderData = that.data.isseuGrider
        let data = {
          id: 0,
          name: "全部人员"
        }
        console.log('isseuGriderData1', isseuGriderData)
        console.log('isseuGrider1', data)
        isseuGriderData.push(data)
        that.setData({
          isseuGriderList: r.data.data,
          isseuGrider: isseuGriderData
        })
      }
    })
  },

  //弹出选择网格员遮罩层
  isseuGriderChage(e) {
    let that = this
    let griderChageIndex = e.currentTarget.dataset.index
    let id = 0;
    console.log("that.grider", that.data.grider)
    if (griderChageIndex == 0) {
      id = that.data.grider.id
    } else {
      id = that.data.isseuGrider[griderChageIndex - 1].id
    }
    util.apiRequest(GetIsseuGriderList, {
      id: id
    }, null).then(r => {
      console.log('下级人员1', r)
      if (r.data.data.length > 0) {
        that.setData({
          isseuGriderList: r.data.data,
          showGriderView: false,
          griderChageIndex: griderChageIndex
        })
      }
    })
  },

  //网格员选择确定
  griderSetm(e) {
    let that = this;
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.title
    let griderChageIndex = that.data.griderChageIndex;
    let isseuGrider = that.data.isseuGrider;
    let grider_id = id
    isseuGrider[griderChageIndex] = {
      id: id,
      name: name
    }
    if (id == 0) {
      if (griderChageIndex - 1 < 0) {
        grider_id = that.data.grider.id
      } else {
        grider_id = isseuGrider[griderChageIndex - 1].id
      }

    }
    util.apiRequest(GetIsseuGriderList, {
      id: id
    }, null).then(r => {
      console.log('下级人员1', r)
      if (r.data.data.length > 0) {
        let data = {
          id: 0,
          name: "全部人员"
        }
        let isseuGriderNew = [];
        for (var i = 0; i <= griderChageIndex; i++) {
          isseuGriderNew[i] = isseuGrider[i]
        }
        console.log("isseuGriderNew", isseuGriderNew)
        isseuGriderNew[griderChageIndex + 1] = data
        console.log("isseuGriderNew111", isseuGriderNew)
        that.setData({
          isseuGriderList: r.data.data,
          isseuGrider: isseuGriderNew,
        })

      } else {
        let isseuGriderNew = [];
        for (var i = 0; i <= griderChageIndex; i++) {
          isseuGriderNew[i] = isseuGrider[i]
        }
        that.setData({
          isseuGrider: isseuGriderNew,
        })
      }
      that.setData({
        showGriderView: true,
        grider_id: grider_id,
        lists: []
      })
      console.log("isseuGrider111", that.data.isseuGrider)
      that.getList();
    })

  },

  getYearMonth() { // 计算当前获取年月日
    var myDate = new Date();
    var tYear = myDate.getFullYear();
    var tMonth = myDate.getMonth();
    var tday = myDate.getDate()

    var m = tMonth + 1;
    if (m.toString().length == 1) {
      m = "0" + m;
    }

    if (tday.toString().length == 1) {
      tday = "0" + tday;
    }

    let endDate = tYear + '-' + m + '-' + tday;
    let month = tYear + '-' + m;

    console.log('当前日期', endDate)
    this.setData({
      time: month,
    })
  },

  close: function() { // 关闭弹窗
    let that = this;
    that.setData({
      showView: true,
      showViewStatus: true,
      showGriderView: true
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var page = this.data.page;

    that.setData({
      isReachBtm: true
    })
    if (that.data.isBottom) return;

    page++;
    that.setData({
      page: page
    })

    this.getList()
  },

  checkDetail(e) {
    let id = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.status;
    let type = e.currentTarget.dataset.type;

    let ucid_name = e.currentTarget.dataset.ucid_name;
    wx.navigateTo({
      url: '/pages/grid/unresolvedDetail/unresolvedDetail?id=' + id + '&status=' + status + '&type=' + type
    })

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})